package com.asm.controller;


import com.asm.mapper.SnvCpgAllMapper;
import com.asm.pojo.BrowseCount;
import com.asm.pojo.SnvCpgAll;
import com.asm.service.SnvCpgAllService;
import com.asm.util.DatatableResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class SnvCpgController {
    private SnvCpgAllService snvCpgAllService;

    public SnvCpgController() {
    }

    @Autowired
    private SnvCpgController(SnvCpgAllService snvCpgAllService) {
        this.snvCpgAllService = snvCpgAllService;
    }

    @Autowired
    private SnvCpgAllMapper snvCpgAllMapper;


    @GetMapping("/snv_cpg")
    public DatatableResult<SnvCpgAll> snvCpgAllList(
            @RequestParam("tissue") String tissue,
            @RequestParam("sampleType") String sampleType,
            @RequestParam("chr") String chr,
            @RequestParam("start") int start,
            @RequestParam("length") int length){
        DatatableResult<SnvCpgAll> datatableResult = new DatatableResult<>();
        PageHelper.startPage(start, length);
        System.out.println("tissue: " + tissue);
        System.out.println("sampleType: " + sampleType);
        System.out.println("chr: " + chr);
        String d3d = null;
        String atac = null;
        String enhancer = null;
        String se = null;
        String insulator = null;

//        根据类型 动态sql
        switch (chr){
            case "3D interaction": d3d = "1"; break;
            case "Accessible chromatin": atac = "1"; break;
            case "Enhancer": enhancer = "1"; break;
            case "Super enhancer": se = "1"; break;
            case "Insulator": insulator = "1"; break;
        }

        List<SnvCpgAll> list = snvCpgAllService.selectAllSnvCpgAll(tissue, sampleType, d3d,atac,enhancer,se,insulator);
        PageInfo<SnvCpgAll> pageInfo = new PageInfo<>(list);
        datatableResult.setDraw(datatableResult.getDraw());
        datatableResult.setData(pageInfo.getList());
        datatableResult.setRecordsTotal((int) pageInfo.getTotal());
        datatableResult.setRecordsFiltered(datatableResult.getRecordsTotal());
        return datatableResult;
    }

    @GetMapping("/getElementByTissue")
    public List<BrowseCount> getElementByTissue(@RequestParam("tissue")String tissue){
        List<BrowseCount> result = snvCpgAllMapper.getElementByTissue(tissue);
        return result;
    }

    /**
     * Search snv and CpG By CpG_id
     *
     * @param start   start
     * @param length  length
     * @param cpgId cpgId
     * @return DatatableResult<SnvCpgAll>
     */
    @GetMapping("/snv_cpg/cpgId")
    public DatatableResult<SnvCpgAll> snvCpgByCpgList(String cpgId,
                                                      @RequestParam("start") int start,
                                                      @RequestParam("length") int length){
        DatatableResult<SnvCpgAll> datatableResult = new DatatableResult<>();
        PageHelper.startPage(start, length);
        List<SnvCpgAll> list = snvCpgAllService.selectAllSnvCpgByCpg(cpgId);
        PageInfo<SnvCpgAll> pageInfo = new PageInfo<>(list);
        datatableResult.setDraw(datatableResult.getDraw());
        datatableResult.setData(pageInfo.getList());
        datatableResult.setRecordsTotal((int) pageInfo.getTotal());
        datatableResult.setRecordsFiltered(datatableResult.getRecordsTotal());
        return datatableResult;
    }

}
